/* eslint-disable */
import tracker from '@UTILS/track'
import Taro from '@tarojs/taro'
import React, { Component } from 'react'
import { Provider } from 'react-redux'
import { PersistGate } from 'redux-persist/integration/react'
import { getPlatform } from '@UTILS/common'
import { setAcquireChannelInfo } from '@UTILS/user'
import Main from './Main'
import configStore from './redux/store'
import './app.scss'
import DBConfig from "@CONFIG/DBConfig"
import api from '@API'

if (process.env.TARO_ENV === 'h5' && (process.env.NODE_ENV !== 'production' || DBConfig.careEnvType === 'qa')) {
  const VConsole = require('vconsole');
  new VConsole();
}
const isWeb = process.env.TARO_ENV === 'h5'
const { store, persistor } = configStore()

global.Date = global.Date || Date
// eslint-disable-next-line no-undef
global.IS_HN_APP = APP_TYPE === 'hn'

const analyticsId = isWeb ? DBConfig.h5AnalyticsId : DBConfig.weAppAnalyticsId
let withCustomTabBarH5
if(process.env.TARO_ENV === 'h5'){
  withCustomTabBarH5 = require('@HOC/withCustomTabBar').default
}
Taro.tracker = tracker;
if (!Object.entries) {
  Object.entries = function (obj) {
    const ownProps = Object.keys(obj)
    let i = ownProps.length;
    const resArray = new Array(i)
    while (i--) resArray[i] = [ownProps[i], obj[ownProps[i]]]
    return resArray;
  };
}

class App extends Component {
  constructor (props) {
    super(props)
    this.props.getInstance?.(this)
  }

  async componentDidMount() {
    tracker.init(analyticsId)
    this.initCloud()
  }

  async onLaunch(options) {
    setAcquireChannelInfo(options)
  }

  initCloud() {
    if (!getPlatform().isWX) return
    Taro.cloud.init({
      env: DBConfig.cloudID,
      traceUser: true
    })
  }
  setAppStatus =async (isInApp)=>{
    try {
      await api.setAppStatus({ InAppStatus : isInApp })
    }catch (e) {}
  }
  componentDidShow() {
    this.setAppStatus(true)
  }

  componentDidHide() {
    this.setAppStatus(false)
  }

  componentDidCatchError() {}

  // 在 App 类中的 render() 函数没有实际作用
  // 请勿修改此函数
  render() {
    const { children, user } = this.props
    return isWeb ? (
      <Provider store={store}>
        <PersistGate loading={null} persistor={persistor}>
          <Main>
            {children}
          </Main>
        </PersistGate>
      </Provider>
    ) : (
      <Provider store={store}>
        <Main>
          {children}
        </Main>
      </Provider>
    )
  }
}

const AppWrapper = isWeb ? withCustomTabBarH5(App) : App

export default AppWrapper
